<template>
  <div class="app-container">
    <!-- 查询 -->
    <el-form  :inline="true" :model="query" size="mini"  class="demo-form-inline">
      <el-form-item label="用户名：">
        <el-input v-model="query.username" ></el-input>
      </el-form-item>
      <el-form-item label="手机号：">
        <el-input v-model="query.mobile" ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button v-permission="'user:search'" icon='el-icon-search' type="primary" @click="queryData">查询</el-button>
        <el-button icon='el-icon-refresh' @click="reload">重置</el-button>
        <el-button v-permission="'user:add'" icon="el-icon-circle-plus-outline" type="primary" @click="openAdd">新增</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table
      :data="list"
      border
      stripe
      style="width: 100%">
      <el-table-column
        align="center"
        type=index
        label="序号"
        width="50">
      </el-table-column>
      <el-table-column 
        align="center"
        prop="username"
        label="用户名"
        >
      </el-table-column>
      <el-table-column
        align="center"
        prop="nickName"
        label="昵称"
        >
      </el-table-column>
      <el-table-column
        align="center"
        prop="mobile"
        label="手机号"
        >
      </el-table-column>
      <el-table-column 
        align="center"
        prop="email"
        label="邮箱"
        >
      </el-table-column>
      <el-table-column
        align="center"
        prop="isAccountNonExpired"
        label="账号过期"
        >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isAccountNonExpired===1">
              正常
          </el-tag>
          <el-tag type="danger" v-if="scope.row.isAccountNonExpired===0">
              过期
          </el-tag>
        </template>                
      </el-table-column>
      <el-table-column
        align="center"
        prop="isAccountNonLocked"
        label="账号锁定"
        >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isAccountNonLocked===1">
              正常
          </el-tag>
          <el-tag type="danger" v-if="scope.row.isAccountNonLocked===0">
              锁定
          </el-tag>
        </template>                
      </el-table-column>
      <el-table-column
        align="center"
        prop="isCredentialsNonExpired"
        label="密码过期"
        >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isCredentialsNonExpired===1">
              正常
          </el-tag>
          <el-tag type="danger" v-if="scope.row.isCredentialsNonExpired===0">
              过期
          </el-tag>
        </template>                
      </el-table-column>
      <el-table-column
        align="center"
        prop="isEnabled"
        label="是否可用"
        >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isEnabled===1">
              可用
          </el-tag>
          <el-tag type="danger" v-if="scope.row.isEnabled===0">
              已删除
          </el-tag>
        </template>                
      </el-table-column>
      <el-table-column  
        align="center"   
        label="操作"
        width="340px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            v-permission="'user:edit'"
            @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            v-permission="'user:delete'"
            @click="handleDelete(scope.row.id)">删除</el-button>
            <el-button
            size="mini"
            type="primary"
            v-permission="'user:role'"
            @click="handleRole(scope.row.id)">设置角色</el-button>
          <el-button
            size="mini"
            type="primary"
            v-permission="'user:password'"
            @click="handlePwd(scope.row.id)">密码修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页功能 -->   
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.current"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total">
    </el-pagination>
    <!-- 弹出窗口 -->
    <edit :visible="edit.visible" :formData="edit.formData" :remoteClose="remoteClose" :title="edit.title" ></edit>
    <!-- 设置角色 -->
    <el-dialog
      title="设置角色"
      :visible.sync="role.visible"
      width="60%"
      >
      <role :roleIds="role.roleIds"  @saveUserRole="saveUserRole">        
      </role>
    </el-dialog>
    <!-- 修改密码 -->
    <password :visible="pwd.visible" :remoteClose="remotePwdClose" title="修改密码" :userId="pwd.userId"></password>
  </div>
</template>

<script>
import * as api from '@/api/user'
import Edit from './edit.vue'
import Role from '../role'
import Password from './password.vue'
export default {
  name:'User',
  components: { Edit , Role ,Password},

  data() {
    return {
      list:[],
      page:{ // 分页相关
        total:0, // 总记录数
        size:20,// 每页显示20条数据
        current:1// 当前页码
      },
      query:{},// 查询条件
      edit:{
        visible:false,
        formData:{
          
        },
        title:''
      },
      // 角色组件
      role:{
        userId:null,
        roleIds: [],// 封装用户拥有角色ids，传递给角色列表子组件
        visible:false
      },
      // 修改密码
      pwd:{
        userId:null,
        visible:false
      }
    }
  },
  methods: {
    // 请求数据
    fetchData(){
      api.getList(this.query,this.current,this.size).then(
        response=>{
          this.list=response.data.records
          this.page.total=response.data.total
          
        }
      )
    },
    // 编辑
    handleEdit(id){
      api.getById(id).then((response)=>{
          
          if (response.code === 20000){
            this.edit.formData=response.data
            this.edit.title = '编辑'
            this.edit.visible = true
            
          }
        })
    },
    // 删除
    handleDelete(id) {
      this.$confirm('确认删除这条记录吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          api.deleteById(id).then((response)=>{
              this.$message({
              type: response.code===20000?'success':'error',
              message: response.message
            });
          })
          this.fetchData()
          
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    // 设置角色
    handleRole(id){
      this.role.userId=id
      api.getRoleIdsByUserId(id).then(
        response=>{
          this.role.visible=true
          this.role.roleIds=response.data
        }
      )
      
    },
    // 密码修改
    handlePwd(id){
      this.pwd.userId=id
      this.pwd.visible=true
    },
    //修改个数
    handleSizeChange(val) {       
        this.page.size=val
        this.fetchData()
    },

    // 修改当前页
    handleCurrentChange(val) {      
      this.page.current=val
      this.fetchData()
    },

    // 查询功能
    queryData() {
        this.page.current=1
        this.fetchData()
    },
    // 重载
    reload(){
      this.query={}
      this.fetchData()
    },
    // 弹出新增窗口
    openAdd(){
      this.edit.visible=true
      this.edit.title='新增——默认密码与用户名一致'
      
    },
    // 触发关闭弹出的新增修改子组件窗口
    remoteClose(){
      this.edit.visible=false
      this.edit.formData={}
      this.fetchData()

    },
    // 触发关闭弹出的密码修改子组件窗口
    remotePwdClose(){
      this.pwd.userId = null
      this.pwd.visible=false
      this.fetchData()
    },
    // 提交设置的用户角色数据
    saveUserRole(ids) {
      api.saveUserRole(this.role.userId,ids).then(response=>{
        if(response.code===20000){          
            this.$message({
            message: '分配角色成功',
            type: 'success'
          });
        }else{
          this.$message({
            message: '分配角色失败',
            type: 'warning'
          });
        }
        this.role.visible=false
      })
    }

  },
  created() {
    this.fetchData()
  },
  
}
</script>

<style>

</style>